function create_textbar(e, collectionid)
{
  $('#collection_title_error').remove();

  currentTitle = $(e).parent().html();
  currentTitle = currentTitle.substring(0,currentTitle.indexOf("<"));

  type = $(e).parent().attr('id');
  e = $('#' + type);

  switch(type)
  {
    case('collection_title'):
      e.html('<input type="text" name="newTitle" value="'+currentTitle+'" maxlength="30">');
      e.find('input').keypress(function(e)
                                    {
                                      if(e.which == 13)
                                      {
                                        save_textbar($(this).parent(), collectionid);
                                      }
                                    }
                                  );
      e.find('input').focus();
      break;
    case('collection_description'):
      e.html('<textarea maxlength="100">'+currentTitle+'</textarea>');
      e.find('textarea').keypress(function(e)
                                    {
                                      if(e.which == 13)
                                      {
                                        save_textbar($(this).parent(), collectionid);
                                      }
                                    }
                                  );
      e.find('textarea').focus();
      break;
    default:
      break;
  }
}

function save_textbar(e, collectionid)
{
  type = $(e).attr('id');

  try {
    ajaxRequest = new XMLHttpRequest();

    switch(type)
    {
      case('collection_title'):
        value = $(e).find('input').attr('value');
        
        ajaxRequest.onreadystatechange = function()
        {
          if(ajaxRequest.readyState == 4)
          {
            if(value != ajaxRequest.responseText)
            {
              value = ajaxRequest.responseText;
              $(e).after('<p id="collection_title_error">Invalid name</p>');
            }
            $(e).html(value + '<img height="15" src="images/edit.jpg" valign="top" onclick="create_textbar(this, '+collectionid+')">');
          }
        }
        url = "./php_scripts/update_collection.php?collectionid="+collectionid+"&changeName="+value;
        ajaxRequest.open("GET", url, true);
        ajaxRequest.send(null);
        break;
      case('collection_description'):
        value = $(e).find('textarea').val();

        ajaxRequest.open("GET", "./php_scripts/update_collection.php?collectionid="+collectionid+"&changeDescription="+value, true);
        ajaxRequest.send(null);

        $(e).html(value + '<img height="15" src="images/edit.jpg" valign="top" onclick="create_textbar(this, '+collectionid+')">');
        break;
      default:
        break;
    }
  } catch (error) {
    alert("error");
  }
}
